home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / xnot12a.zip / DOS.C < prev    next >
C/C++ Source or Header  |  1993-05-20  |  8KB  |  347 lines

  1. #include "jam.h"
  2. #include "def.h"
  3.  
  4. /* Stuff split out of file.c and fileio.c; makes DOS/NT calls
  5. * which depend (kinda) on FAT as the filesytem type.
  6. */
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9.   
  10. #include "stdio.h"
  11. #include "direct.h"
  12. #include "dos.h"
  13. #include "io.h"
  14.   
  15. static BOOL details_in_dir = FALSE;
  16.  
  17. /* Get directory listing from partial spec; *.* added to
  18.  * end, but no checking done otherwise
  19.  */
  20. void GetDiskDirectory(BUFFER *bp, char *path)
  21. {
  22. #define DOSSPEC (8+3+1+1+1)  /* "12345678.123 \0" */
  23. #define DOSLEN (DOSSPEC + 3)
  24.   
  25.   char dosspec[NFILEN + 1];
  26. #ifdef WIN32
  27.   WIN32_FIND_DATA fileinfo;
  28.   HANDLE context;
  29. #else
  30.   struct find_t fileinfo;
  31. #endif
  32.   int  numfiles = 0;
  33.   char *wild = "*.*";
  34.   char *unknown = "-";
  35.   char buffer[2*NCOL], newpath[NFILEN];
  36.   register int i, j, k;
  37.   struct stat statbuf;
  38.   BOOL found = FALSE;
  39.   BOOL goodname;
  40.   
  41. #ifdef WINDOWED
  42.   WindowSleepCursor();
  43. #endif
  44.   
  45.   /* need wildcard stuff, don't add if supplied
  46.    */
  47.   strcpy(newpath, path);
  48.   k = (int)strlen(newpath);
  49.   for (i = 0; i < k; i++)
  50.     {
  51.       if (newpath[i] == '*')
  52.         {
  53.           found = TRUE;
  54.           break;
  55.         }
  56.     }
  57.   if (!found)
  58.     strcat(newpath, wild);
  59.   if (newpath[k-1] == '*')    /* ends with *, is it complete? */
  60.     if (newpath[k-2] != '.')
  61.       strcat(newpath, wild);    /* will have **.* after this, but ok */
  62.   
  63.   
  64.   /* Set up names per row - NOTE NEED CHECK FOR 
  65.    * NONE-FAT FILESYSTEM NAME LENGTH here else UGHLY!
  66.    */
  67.   if (details_in_dir)
  68.     k = 1;
  69.   else
  70.     k = ncol/(DOSLEN-1);
  71.   
  72.   /* Find the first file which matches the constructed path
  73.    */
  74. #ifndef WIN32
  75.   if (_dos_findfirst(newpath, _A_NORMAL, &fileinfo) == 0)
  76. #else
  77.   if (context = FindFirstFile(newpath, &fileinfo))
  78. #endif
  79.     {
  80.       i = 0;
  81.       buffer[0] = '\0';
  82.       for (; ;)
  83.     {
  84.       numfiles++;
  85.       
  86.       /* do K names per row
  87.        */
  88.           memset(dosspec, '\0', NFILEN + 1);    /* zero pad large buffer */
  89.       memset(dosspec, ' ', DOSSPEC+2);
  90.       dosspec[DOSLEN-1] = '\0';
  91. #ifndef WIN32
  92.       for (j = 0; fileinfo.name[j]; j++)
  93.         dosspec[j+1] = fileinfo.name[j];
  94. #else
  95.       for (j = 0; fileinfo.cFileName[j]; )
  96.             {
  97.           dosspec[j+1] = fileinfo.cFileName[j];
  98.               j++;
  99.               if ((j == NFILEN)    /* buffer full, leave trailing null */
  100.                 break;
  101.             }
  102. #endif
  103.       /* step over these dir (not useful) names (HACK)
  104.        */
  105.       if (dosspec[1] == '.')
  106.         {
  107.           if ((dosspec[2] == '.') || (dosspec[2] == ' '))
  108.         goodname = FALSE;;
  109.         }
  110.           else
  111.             goodname = TRUE;
  112.  
  113.       if (goodname)
  114.         {
  115.           adjustnamecase(dosspec);
  116.           strcat(buffer, dosspec);
  117.           
  118.           /* read/write/exec info in buffer 
  119.            */
  120.           if (details_in_dir)
  121.         {
  122.           char temp[NFILEN];
  123.           
  124.           strcpy(temp, path);
  125. #ifndef WIN32
  126.           strcat(temp, fileinfo.name);
  127. #else
  128.           strcat(temp, fileinfo.cFileName);
  129. #endif
  130.           /* use stat - get r/w/x state
  131.            */
  132.           if (!stat(temp, &statbuf))
  133.             {
  134.               if (statbuf.st_mode & S_IREAD)
  135.             strcat(buffer, "r");
  136.               else 
  137.             strcat(buffer, unknown);
  138.               if (statbuf.st_mode & S_IWRITE)
  139.             strcat(buffer, "w");
  140.               else 
  141.             strcat(buffer, unknown);
  142.               if (statbuf.st_mode & S_IEXEC)
  143.             strcat(buffer, "x");
  144.               else 
  145.             strcat(buffer, unknown);
  146.             }
  147.           else
  148.             strcat(buffer, "???");       /* read/write/exec ? */
  149.         }
  150.           i++;
  151.           
  152.           if (i >= k)
  153.         {
  154.           register int n = strlen(buffer) - 1;
  155.           
  156.           while (buffer[n] == ' ')
  157.             n--;
  158.           if (n >= 0)
  159.             buffer[n+1] = 0;        /* kill extra blanks at lines end */
  160.           
  161.           addline(bp, buffer);  
  162.           buffer[0] = '\0';
  163.           i = 0;
  164.         }
  165.         } /* goodname */
  166.       
  167.       /* Find next file which matches path, else
  168.        * terminate and break loop
  169.        */
  170. #ifndef WIN32
  171.       if (_dos_findnext(&fileinfo))
  172. #else
  173.       if (!FindNextFile(context, &fileinfo))
  174. #endif
  175.         {
  176.               if (i)
  177.                 addline(bp, buffer);   /* flush last line */
  178.           break;
  179.         }
  180.     }
  181.     }
  182.   
  183.   ewprintf("%d files found", numfiles);
  184. #ifdef WINDOWED
  185.   WindowNormalCursor();
  186. #endif
  187. }
  188.  
  189. /* Both were in fileio.c but I moved them here
  190. * because they make dos calls (JAM).
  191. *
  192. * NOTE the hack for returning disk drive on NT.
  193. * 'wdir' is current path and so better have
  194. * a drive letter in first char! If not, this code
  195. * will break.
  196. *
  197. * DOS has notion of current directory per drive...
  198. */
  199. unsigned short getdisk()
  200. {
  201. #ifdef WIN32  
  202.   if (wdir[0])
  203.     return ((unsigned short)(wdir[0] - 'a'));
  204. #else
  205.   unsigned int currentdrive;
  206.   
  207.   _dos_getdrive(¤tdrive);
  208.   return currentdrive-1;
  209. #endif
  210. }
  211.  
  212. /* DOS has notion of current directory per drive...
  213.  */
  214. void setdisk(char c)
  215. {
  216. #ifndef WIN32 /* NOOP for NT */
  217.   unsigned number_of_drives;
  218.   
  219.   _dos_setdrive((unsigned short)(c - 'a' + 1), &number_of_drives);
  220. #endif
  221. }
  222.  
  223. /* Return into buffer of the requested drive 
  224.  * in the string; note it does a hacky thing in DOS
  225.  * to set that drive, then fetch the cwd and then resets
  226.  * the drive.
  227.  *
  228.  * DOS has notion of current directory per drive...
  229.  */
  230. void getcurdir(unsigned short drivenumber, char *buf)
  231. {
  232. #ifdef WIN32
  233.   char *temp;
  234.   temp = getwd(wdir);
  235.   strcpy(&buf[3], &temp[3]);
  236. #else
  237.   unsigned int currentdrive = (int)(getdisk()+1);
  238.   unsigned int number_of_drives;
  239.   char bufr[NFILEN];
  240.   
  241.   _dos_setdrive(drivenumber, &number_of_drives);
  242.   getcwd(&bufr[0], NFILEN-1);
  243.   _dos_setdrive(currentdrive, &number_of_drives);
  244.   strcpy(buf, &bufr[3]);
  245. #endif
  246. }
  247.  
  248. /* Should always use stat? No, DOS is weird
  249.  * and stat fails sometimes. This works best
  250.  * and there is a NT call for it also.
  251.  */
  252. BOOL fileisrdonly(char *s)
  253. {
  254.   BOOL result = FALSE;
  255. #ifndef WIN32
  256.   unsigned int attr = 0;
  257.   
  258.   if (s && *s)
  259.     _dos_getfileattr(s, &attr);
  260.   if (attr & _A_RDONLY)
  261.     result = TRUE;
  262. #else
  263.   DWORD foo;
  264.   
  265.   if ((foo = GetFileAttributes(s)) >= 0)
  266.     result = (foo & FILE_ATTRIBUTE_READONLY ? TRUE: FALSE);
  267.   
  268. #endif
  269.   return(result);
  270. }
  271.  
  272. /* Dummie routines I was playing with and are not used at
  273. * all anymore. I should delete them...
  274. */
  275. #if 0
  276. dosdir(f, n)
  277. int f, n;
  278. {
  279.   extern BOOL details_in_dir;
  280.   char dirn[NFILEN];
  281.   int s;
  282.   BUFFER *bp;
  283.   extern BUFFER *dired_(char *s);
  284.   
  285.   epreload(dirpath());
  286.   if ((s=eread("DOS directory: ", dirn, NFILEN, EFNEW|EFBUF) == ABORT))
  287.     return FALSE;
  288.   strcat(dirn, "\\");
  289.   details_in_dir = TRUE;
  290.   if (bp = dired_(dirn))
  291.     {
  292.       bp->b_flag |= BFVIEW;
  293.       curbp = bp;
  294.       bp->b_dotp = lforw(bp->b_linep);        /* go to first line */
  295.     }
  296.   details_in_dir = FALSE;
  297.   return(bp ? showbuffer(bp, curwp, WFHARD | WFMODE) : FALSE);
  298. }
  299. dosset(f, n)
  300. int f, n;
  301. {
  302.   char bufn[NFILEN];
  303.   int s;
  304.   
  305.   if ((s=eread("File to change read/write access: ", bufn, 
  306.            NFILEN, EFNEW|EFBUF) == ABORT))
  307.     return FALSE;
  308.   if ((s=eyesno("Set readonly")) != TRUE)
  309.     if (s == ABORT)
  310.       return FALSE;
  311.   if (s)
  312.     _dos_setfileattr(bufn, _A_RDONLY);
  313.   else
  314.     _dos_setfileattr(bufn, _A_NORMAL);
  315.   
  316.   return TRUE; 
  317. }
  318. dosget(f, n)
  319. int f, n;
  320. {
  321.   char bufn[NFILEN];
  322.   int s;
  323.   unsigned int attr = (unsigned int)(~_A_NORMAL);
  324.   
  325.   s=eread("Try to open for write: ", bufn, NFILEN, EFNEW|EFBUF);
  326.   _dos_getfileattr(bufn, &attr);
  327.   
  328.   if (attr & _A_RDONLY)
  329.     ewprintf("file is readonly");
  330.   else if (attr & _A_HIDDEN) 
  331.     ewprintf("file is readonly");
  332.   else if (attr == _A_NORMAL)
  333.     ewprintf("file is normal");
  334.   else if (attr & _A_SYSTEM)
  335.     ewprintf("file is system");
  336.   else if (attr & _A_VOLID)
  337.     ewprintf("file is vol id!?");
  338.   else if (attr & _A_SUBDIR)
  339.     ewprintf("file is subdir");
  340.   else if (attr & _A_ARCH)
  341.     ewprintf("file is archive");
  342.   
  343.   return TRUE; 
  344. }
  345. #endif
  346.  
  347.